
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class UserModify extends JFrame{
    public UserModify(String userid, Object uUID){

        SQLOperation sql=new SQLOperation();
        setTitle("Family Modify");
        Container container=getContentPane();
        setLayout(new BorderLayout());

        JPanel titlePanel=new JPanel();

        JLabel title=new JLabel("Modify Information of "+uUID);
        title.setFont(new Font("",Font.BOLD,20));

        titlePanel.add(title);

        add(titlePanel,BorderLayout.NORTH);




        JPanel select=new JPanel();

        JComboBox attributeSelect=new JComboBox();
        attributeSelect.addItem("----------");
        attributeSelect.addItem("phoneNum");
        attributeSelect.addItem("gender");
        attributeSelect.addItem("dateOfBirth");

        JTextField enter=new JTextField(20);

        select.add(attributeSelect);
        select.add(enter);

        add(select,BorderLayout.CENTER);

        JButton back=new JButton("Back");
        back.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                new FamilySearchResult(userid,uUID);
            }
        });

        JButton renew=new JButton("Initialize");
        renew.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                int renewOperation=JOptionPane.showConfirmDialog
                        (null, "Confirm Initialize?","Confirm",JOptionPane.YES_NO_OPTION);

                if(renewOperation==JOptionPane.YES_OPTION){
                    String randomPass="";
                    for(int i=1;i<=8;i++){
                        randomPass=randomPass+(char)((int)(Math.random()*26)+97);
                    }
                    sql.modify("User","uUID",uUID,"Password",randomPass);
                    sql.modify("Account","UID",uUID,"Password",randomPass);

                    JOptionPane.showMessageDialog
                            (null, "Initialize Password Complete!","Success",JOptionPane.INFORMATION_MESSAGE);



                }

            }
        });
        JButton modify=new JButton("Modify it");
        modify.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(attributeSelect.getSelectedIndex()==0||enter.getText().equals("")){
                    JOptionPane.showMessageDialog
                            (null, "Fail Modify Update!","Wrong Input",JOptionPane.ERROR_MESSAGE);

                }
                else{
                    sql.modify("User","uUID",uUID,
                            (String)attributeSelect.getSelectedItem(),enter.getText());
                    JOptionPane.showMessageDialog
                            (null, "Modify Complete!","Success",JOptionPane.INFORMATION_MESSAGE);
                    setVisible(false);
                    new UserSearchResult(userid,uUID);

                }


            }
        });



        JPanel buttons=new JPanel();
        buttons.add(back);
        buttons.add(renew);
        buttons.add(modify);

        add(buttons,BorderLayout.SOUTH);



        setSize(400,150);
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        setVisible(true);

    }
}
